package flc.ast.activity;

import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.bean.ResumeBean;
import flc.ast.databinding.ActivityOtherInfoBinding;
import flc.ast.dialog.TitleRenameDialog;
import flc.ast.utils.ModuleCategory;
import flc.ast.utils.ResumeDataUtils;
import stark.common.basic.event.EventStatProxy;

public class OtherInfoActivity extends BaseAc<ActivityOtherInfoBinding> {

    public static String moduleCategory;
    public static int position;
    public static int modulePos;
    private ResumeBean resumeBean;
    private ResumeBean.Module module;
    private ResumeBean.SelfEvaluationData selfEvaluationData;
    private ResumeBean.HobbyData hobbyData;
    private ResumeBean.CustomModuleData customModuleData;

    @Override
    protected int onCreate() {
        return R.layout.activity_other_info;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivOtherInfoBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvSave.setOnClickListener(this);
        mDataBinding.tvTitle.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvSave://保存
                save();
                break;
            case R.id.tvTitle://标题
                showRenameDialog();
                break;
        }
    }

    @Override
    protected void initData() {
        resumeBean = ResumeDataUtils.getResumeList().get(position);
        module = resumeBean.getResumeData().get(modulePos);
        mDataBinding.tvTitle.setText(module.getAlias());
        switch (ModuleCategory.fromCode(moduleCategory)) {
            case HOBBIES_INTERESTS://兴趣爱好
                mDataBinding.tvTip.setText(R.string.hobbies_interests_tip);
                mDataBinding.etDetail.setHint(getString(R.string.hobbies_interests_hint));
                if (module.getData() != null) {
                    hobbyData = GsonUtils.fromJson(module.getData().toString(), ResumeBean.HobbyData.class);
                    String detail = "";
                    hobbyData.setDetailList(GsonUtils.fromJson(hobbyData.getDetailList().toString(), new TypeToken<List<ResumeBean.DetailItem>>() {
                    }.getType()));
                    for (ResumeBean.DetailItem detailItem : hobbyData.getDetailList()) {
                        detail += detailItem.getText() + "\n";
                    }
                    mDataBinding.etDetail.setText(detail);
                } else {
                    hobbyData = new ResumeBean.HobbyData();
                }
                break;
            case SELF_EVALUATION://自我评价
                mDataBinding.tvTip.setText(R.string.self_evaluation_tip);
                mDataBinding.etDetail.setHint(getString(R.string.self_evaluation_hint));
                if (module.getData() != null) {
                    selfEvaluationData = GsonUtils.fromJson(module.getData().toString(), ResumeBean.SelfEvaluationData.class);
                    String detail = "";
                    selfEvaluationData.setDetailList(GsonUtils.fromJson(selfEvaluationData.getDetailList().toString(), new TypeToken<List<ResumeBean.DetailItem>>() {
                    }.getType()));
                    for (ResumeBean.DetailItem detailItem : selfEvaluationData.getDetailList()) {
                        detail += detailItem.getText() + "\n";
                    }
                    mDataBinding.etDetail.setText(detail);
                } else {
                    selfEvaluationData = new ResumeBean.SelfEvaluationData();
                }
                break;
            case CUSTOM_TITLE://自定义标题
                mDataBinding.tvTip.setText(R.string.custom_title);
                mDataBinding.etDetail.setHint(getString(R.string.content_hint));
                if (module.getData() != null) {
                    customModuleData = new Gson().fromJson(module.getData().toString(), ResumeBean.CustomModuleData.class);
                    String detail = "";
                    customModuleData.setDetailList(GsonUtils.fromJson(customModuleData.getDetailList().toString(), new TypeToken<List<ResumeBean.DetailItem>>() {
                    }.getType()));
                    for (ResumeBean.DetailItem detailItem : customModuleData.getDetailList()) {
                        detail += detailItem.getText() + "\n";
                    }
                    mDataBinding.etDetail.setText(detail);
                } else {
                    customModuleData = new ResumeBean.CustomModuleData();
                }
                break;
        }
    }

    private void save() {
        String detail = mDataBinding.etDetail.getText().toString().trim();
        if (TextUtils.isEmpty(detail)) {
            switch (ModuleCategory.fromCode(moduleCategory)) {
                case HOBBIES_INTERESTS://兴趣爱好
                    ToastUtils.showShort(getString(R.string.please_input_hobbies_interests));
                    break;
                case SELF_EVALUATION://自我评价
                    ToastUtils.showShort(getString(R.string.please_input_self_evaluation));
                    break;
                case CUSTOM_TITLE://自定义标题
                    ToastUtils.showShort(getString(R.string.please_input_content));
                    break;
            }
            return;
        }

        String[] lines = detail.split("\\r?\\n");
        List<ResumeBean.DetailItem> list = new ArrayList<>();
        for (String line : lines) {
            list.add(new ResumeBean.DetailItem(line));
        }

        List<ResumeBean> listSave = ResumeDataUtils.getResumeList();
        switch (ModuleCategory.fromCode(moduleCategory)) {
            case HOBBIES_INTERESTS://兴趣爱好
                hobbyData.setDetailList(list);
                hobbyData.setHobbyList(new ArrayList<>());
                module.setData(hobbyData);
                break;
            case SELF_EVALUATION://自我评价
                selfEvaluationData.setDetailList(list);
                module.setData(selfEvaluationData);
                break;
            case CUSTOM_TITLE://自定义标题
                customModuleData.setDetailList(list);
                module.setData(customModuleData);
                break;
        }
        listSave.set(position, resumeBean);
        ResumeDataUtils.saveResumeList(listSave);
        ToastUtils.showShort(getString(R.string.save_success));
        finish();
    }


    private void showRenameDialog() {
        TitleRenameDialog renameDialog = new TitleRenameDialog(mContext);
        renameDialog.text = mDataBinding.tvTitle.getText().toString().trim();
        renameDialog.setListener(new TitleRenameDialog.Ilistener() {

            @Override
            public void clickRename(String name) {
                module.setAlias(name);
                mDataBinding.tvTitle.setText(name);
            }
        });
        renameDialog.show();
    }

}